/**
 * 
 */
package fhq.hcmute.modernshopping.fragment;

import android.app.DialogFragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import fhq.hcmute.modernshopping.ProjectUtil;
import fhq.hcmute.modernshopping.R;
import fhq.hcmute.modernshopping.webservice.Webservice;

/**
 * @author: Hien Nguyen Le
 * @emai: hien.nguyenle.it@gmail.com
 * 
 */
public class CommentDialog extends DialogFragment {
	private EditText edt_username;
	private EditText edt_comment;
	private Button btn_Comment;
	private Button btn_Close;

	private String title;
	private int productCode;

	public CommentDialog(int productCode, String title) {
		this.title = title;
		this.productCode = productCode;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		getDialog().setTitle(this.title);
		View view = inflater.inflate(R.layout.activity_comment_product,
				container, false);
		btn_Close = (Button) view.findViewById(R.id.btn_close);
		btn_Close.setOnClickListener(onCancelClickListener);
		btn_Comment = (Button) view.findViewById(R.id.btn_comment);
		btn_Comment.setOnClickListener(onCommentClickListener);
		edt_username = (EditText) view.findViewById(R.id.txt_username);
		edt_comment = (EditText) view.findViewById(R.id.txt_comment);
		return view;
	}

	private OnClickListener onCancelClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			dismiss();
		}
	};

	private OnClickListener onCommentClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (edt_username.getText().length() == 0) {
				edt_username.setError("Bạn chưa nhập họ tên");
				return;
			}

			if (edt_comment.getText().length() < 50) {
				edt_comment.setError("Vui lòng nhập nhận xét ít nhất 50 kí tự");
				return;
			}

			String comment = edt_comment.getText().toString();
			String username = edt_username.getText().toString();
			comment = comment.replace(" ", "%20");
			username = username.replace(" ", "%20");

			String url = ProjectUtil.WEB_SERVICE_LINK
					+ "CommentProduct?ProductCode=%d&Comment=%s&Username=%s";
			url = String.format(url, CommentDialog.this.productCode, comment,
					username);
			new Webservice(getActivity(), commentProductHandler).execute(url);
		}
	};

	private Handler commentProductHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == Webservice.GET_DATA_SUCCESSFUL) {
				String result = (String) msg.obj;
				if (result.equals("true")) {
					getDialog().dismiss();
					Toast.makeText(getActivity(),
							getString(R.string.toast_comment_success),
							Toast.LENGTH_SHORT).show();
				}
			} else if (msg.what == Webservice.GET_DATA_FAILED) {
				Toast.makeText(getActivity(),
						getString(R.string.toast_comment_failed),
						Toast.LENGTH_SHORT).show();
			}
			super.handleMessage(msg);
		}
	};
}
